package cartFilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/CartServlet")
public class CartFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化。。。");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest hst = (HttpServletRequest) servletRequest;
        Object username = hst.getSession().getAttribute("username");
        System.out.println(username);
        if (username!=null){
            System.out.println("购物车访问成功");
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            System.out.println("请重新登录");
            servletResponse.setContentType("text/html;Charset=utf-8");
            servletResponse.getWriter().println("请重新登录");
        }
    }

    @Override
    public void destroy() {

    }
}
